package controlleur.form;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.InternalResourceView;

import annuaire.rmi.remotedObject.IRemotedAnnuaire;
import annuaire.rmi.remotedObject.IRemotedUser;
import form.bean.ConnectForm;

/**
 * Contrôleur responsable du fomulaire de connexion
 * 
 * @author Yannick Lorenzati & Stephanie Malakian
 * 
 */
public class ConnectFormControlleur extends SimpleFormController {

	/** Logger pour cette classe ainsi que pour ses sous classes */
	protected final Log logger = LogFactory.getLog(getClass());

	/**
	 * Reference vers l'objet distant Annuaire
	 */
	private IRemotedAnnuaire remotedAnnuaire;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax
	 * .servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse,
	 * java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object commandAjout,
			BindException errors) throws Exception {

		// on tente un login
		ConnectForm c = (ConnectForm) commandAjout;
		IRemotedUser user = remotedAnnuaire.login(c.getLogin(), c.getPwd());

		// si echec
		if (user == null) {
			errors.reject("key", "Login ou mot de passe incorrect");
			return showForm(request, response, errors);
		} else {
			// on met l'user logguer en session
			request.getSession().setAttribute("user", user);
			request.getSession().setAttribute("login", c.getLogin());
			request.getSession().setAttribute("role", user.getRole());

			// on construit la vue
			Map<String, Object> myModel = new HashMap<String, Object>();
			myModel.put("url", "/home.htm");
			myModel.put("time", 3);
			myModel
					.put("message",
							"Vous êtes desormais connecté, vous allez être redirigé vers l'accueil...");

			return new ModelAndView(
					new InternalResourceView("jsp/redirect.jsp"), "model",
					myModel);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject
	 * (javax.servlet.http.HttpServletRequest)
	 */
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws ServletException {

		return new ConnectForm();
	}

	/**
	 * @param remotedAnnuaire
	 */
	public void setRemotedAnnuaire(IRemotedAnnuaire remotedAnnuaire) {
		this.remotedAnnuaire = remotedAnnuaire;
	}

	/**
	 * @return remotedAnnuaire
	 */
	public IRemotedAnnuaire getRemotedAnnuaire() {
		return remotedAnnuaire;
	}

}
